package com.cat.greedyAlgorithm;

import java.util.TreeSet;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimize-deviation-in-array/description/
 * @create 2025/10/8 15:41
 * @since JDK17
 */

public class Solution55 {
    public int minimumDeviation(int[] nums) {
        TreeSet<Integer> set = new TreeSet<>();
        for (int num : nums) {
            if (num % 2 == 1) {
                set.add(num * 2);
            } else {
                set.add(num);
            }
        }
        int ans = set.last() - set.first();
        while (!set.isEmpty() && set.last() % 2 == 0) {
            int max = set.last();
            set.remove(max);
            set.add(max / 2);
            ans = Math.min(ans, set.last() - set.first());
        }
        return ans;
    }
}
